package cn.com.weather.nf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.weather.nf.entity.RecordForm;
import cn.com.weather.nf.entity.RecordWeather;
import cn.com.weather.nf.repository.JdbcDao;
import cn.com.weather.nf.service.JcbcService;

@Service
public class JcbcServiceImpl implements JcbcService {

    private static final Logger LOGGER = LoggerFactory.getLogger(JcbcServiceImpl.class );
    
    /** 天气 */
    public static final Integer SCORE_TYPE_TQ = 1;
    /** 问题 */
    public static final Integer SCORE_TYPE_WT = 2;
    /** 意见 */
    public static final Integer SCORE_TYPE_YJ = 3;
    /** 奖励 */
    public static final Integer SCORE_TYPE_JL = 4;

    public static final Integer SCORE_PROCESS_STAT_OK = 1;
    public static final Integer SCORE_PROCESS_STAT_NO = 0;
    
    @Autowired
    private JdbcDao jdbcDao;


    @Override
    public void updateOrAddRecordWeatherScoure(RecordWeather recordWeather) {
        
        String recordTable = "sun_record_weather";
        Long userId = recordWeather.getUser_id();

        if (userId == null) {
            return ;
        }
        
        String userUid = this.jdbcDao.getUserUid(userId);
        Long recordId = recordWeather.getId();
        Long scoreId = this.jdbcDao.getUserScoreId(userUid,recordTable, recordId);
        
        
        Map<String, Object> param = new HashMap<>();
        param.put("score_count", 0); // 没有用
        param.put("score_info", ""); // 同上
        
        param.put("user_uid", userUid);
        param.put("record_table", recordTable);
        param.put("record_id", recordId);
        param.put("create_time", new Date());
        param.put("score_type", SCORE_TYPE_TQ);
        param.put("record_type_name", "天气反馈");
        if (SCORE_PROCESS_STAT_OK.equals(recordWeather.getProcess_stat())) {
            param.put("score_val", SCORE_PROCESS_STAT_OK);
        } else {
            param.put("score_val", SCORE_PROCESS_STAT_NO);
        }
        
        
        if (scoreId != null) {
            // 代表有数据记录， 需要更新
            this.jdbcDao.updateRecordScore(scoreId, param);
        } else {
            // 代表没有数据记录，需要新建 
            this.jdbcDao.insertRecordScore( param);
        }
        
        
    }


    @Override
    public void updateOrAddRecordFormScoure(RecordForm recordForm) {
       
        String recordTable = "sun_record_form";
        Long userId = recordForm.getUser_id();
        
        if (userId == null) {
            return ;
        }
        
        String userUid = this.jdbcDao.getUserUid(userId);
        Long recordId = recordForm.getId();
        Long scoreId = this.jdbcDao.getUserScoreId(userUid,recordTable, recordId);
        
        
        Map<String, Object> param = new HashMap<>();
        param.put("score_count", 0); // 没有用
        param.put("score_info", ""); // 同上
        
        param.put("user_uid", userUid);
        param.put("record_table", recordTable);
        param.put("record_id", recordId);
        param.put("create_time", new Date());
        param.put("score_type", SCORE_TYPE_TQ);
        String recordType = this.jdbcDao.getRecordFormType(recordId);
        if ("yijian".equals(recordType)) {
            param.put("record_type_name", "意见反馈");
        } else {
            param.put("record_type_name", "问题反馈");
        }
        
        if (SCORE_PROCESS_STAT_OK.equals(recordForm.getProcess_stat())) {
            param.put("score_val", SCORE_PROCESS_STAT_OK);
        } else {
            param.put("score_val", SCORE_PROCESS_STAT_NO);
        }
        
        
        if (scoreId != null) {
            // 代表有数据记录， 需要更新
            this.jdbcDao.updateRecordScore(scoreId, param);
        } else {
            // 代表没有数据记录，需要新建 
            this.jdbcDao.insertRecordScore( param);
        }
        
    }
    
    
    
}
